import tkinter as tk
from form_component import FormComponent
from tkinter import ttk
from table_component import TableComponent

class AdminFramework:
    def __init__(self, title):
        self.root = tk.Tk()
        self.root.title(title)
        self.root.geometry('800x600')
        self.setup_theme()

    def setup_theme(self):
        style = ttk.Style()
        # style.theme_use('clam')
        # style.configure('.', font=('Arial', 11), foreground='#333333', background='#ffffff')
        # style.configure('TFrame', background='#f5f5f5')
        # style.configure('TButton', padding=5, relief='flat', background='#0078d7', foreground='#ffffff')
        # style.map('TButton', background=[('active', '#005ba1')])

    def init_ui(self):
        self.main_frame = ttk.Frame(self.root)
        self.main_frame.pack(fill='both', expand=True)

        # 创建菜单栏
        menu_container = ttk.Frame(self.main_frame, style='Menu.TFrame')
        menu_container.pack(side='left', fill='y', padx=10, pady=10)
        style = ttk.Style()
        # style.configure('Menu.TFrame', background='#0078d7', relief='flat')

        logo_frame = ttk.Frame(menu_container, width=15, height=15)
        logo_frame.pack(side='top', fill='none', padx=5, pady=5)
        self.logo_image = tk.PhotoImage(file='logo.png').subsample(5, 5)
        logo_label = ttk.Label(logo_frame, image=self.logo_image)
        logo_label.pack(side='left', anchor='nw')

        self.menu_frame = ttk.Frame(menu_container)
        self.menu_frame.pack(side='left', fill='y', padx=10, pady=10)
        
        # 初始化内容区域
        self.init_content_area()
        
        # 初始化日志区域
        self.init_log_area()
        
        # 调用创建菜单方法
        self.create_menu()
        
    def create_menu(self):
        """
        可重写的菜单创建方法
        子类可以重写此方法来自定义菜单按钮
        """
        # 清空原有菜单按钮
        for widget in self.menu_frame.winfo_children():
            widget.destroy()
            
        # 默认菜单
        ttk.Button(self.menu_frame, text='用户管理', command=self.show_user_management).pack(pady=5)
        ttk.Button(self.menu_frame, text='系统设置', command=self.show_system_settings).pack(pady=5)
        ttk.Button(self.menu_frame, text='卡模板', command=self.show_system_settings).pack(pady=5)
        ttk.Button(self.menu_frame, text='AB面', command=self.show_system_settings).pack(pady=5)
        ttk.Button(self.menu_frame, text='特效', command=self.show_system_settings).pack(pady=5)
        
   

        # 默认显示用户管理界面
        self.show_user_management()

    def show_user_management(self):
        # 清空内容区域
        for widget in self.content_frame.winfo_children():
            widget.destroy()
        defaults = {
        'Email': 'zhangsan@example.com',
        'Name': '张三',
        'Password': '123456',
        'Dir': 'C:/Users/DELL/Videos/虾客/随机ab',
        # 'Avatar': 'C:/Users/DELL/Videos/虾客/随机ab/20250428183433-c_p.mp4',
        # 'Type': 'option1',
        # 'Agree': {'option1': True, 'option2': False}
    }
        # 创建用户管理界面
        def custom_form(dialog, defaults, selected=None,mode='add'):
            form = FormComponent(dialog, defaults)
            form.text('Name', '姓名')
            form.text('Email', '邮箱')
            form.text('Password', '密码')
            form.file('Dir', '目录')
            form.create_form()
            def on_submit():
                data = form.get_data()
                print(mode)
                if all(data.values()):
                    if mode == 'add':
                        table3.add_row(list(data.values()))
                    else:
                        print(list(data.values()))
                        table3.tree.item(selected, values=list(data.values()))
                    dialog.destroy()
                else:
                    messagebox.showwarning('警告', '请填写所有字段')
                
            form.submit(on_submit)
            return form
        
        table3 = TableComponent(self.content_frame, list(defaults.keys()), create_form=custom_form)
        table3.column('Name', '用户名')
        table3.column('Email', '电子邮箱')
        table3.column('Password', '密码')
        table3.add_row(list(defaults.values()))
        self.root.mainloop()

    def show_system_settings(self):
        # 清空内容区域
        for widget in self.content_frame.winfo_children():
            widget.destroy()

        # 创建系统设置界面
        defaults = {
            'video_path': 'C:/Videos',
            'theme': 'light',
            'auto_save': True
        }
        form = FormComponent(self.content_frame, defaults)
        form.dir('video_path', '视频路径')
        form.radio('theme', '主题').option({'light': '浅色', 'dark': '深色'})
        form.checkbox('auto_save', '自动保存').option({'auto_save': '启用自动保存'})
        form.create_form()
        
        def on_submit():
            data = form.get_data()
            self.log_message(f"系统设置已保存: {data}")
            
        form.submit(on_submit)
        form.pack(fill='both', expand=True, padx=20, pady=20)

    def run(self):
        if self.check_activation():
            self.init_ui()
        self.root.mainloop()
        
    def init_content_area(self):
        """
        初始化内容区域
        """
        content_container = ttk.Frame(self.main_frame)
        content_container.pack(fill='both', expand=True)
        
        self.content_frame = ttk.Frame(content_container)
        self.content_frame.pack(fill='both', expand=True)
        
    def init_log_area(self):
        """
        初始化日志区域
        """
        self.log_frame = ttk.LabelFrame(self.main_frame, text='系统日志')
        self.log_frame.pack(side='bottom', fill='x', padx=10, pady=10)
        
        self.log_text = tk.Text(self.log_frame, height=5, state='disabled')
        scrollbar = ttk.Scrollbar(self.log_frame, orient='vertical', command=self.log_text.yview)
        self.log_text.configure(yscrollcommand=scrollbar.set)
        
        # scrollbar.pack(side='right', fill='y')
        self.log_text.pack(side='left', fill='both', expand=True)
        
    def log_message(self, message):
        self.log_text.config(state='normal')
        self.log_text.insert('end', message + '\n')
        self.log_text.see('end')
        self.log_text.config(state='disabled')
        
    def check_activation(self):
        # 隐藏主窗口内容
        for widget in self.root.winfo_children():
            widget.pack_forget()
        
        # 在主窗口创建激活表单
        self.root.title('产品激活')
        activation_frame = ttk.Frame(self.root)
        activation_frame.pack(fill='both', expand=True)
        defaults = {'activation_code': '123456'}
        form = FormComponent(activation_frame, defaults)
        form.text('activation_code', '激活码')
        form.create_form()
        
        # 验证结果状态
        self.verified = False
        
        def on_submit():
            data = form.get_data()
            if data['activation_code'] == '123456':
                self.verified = True
                # 验证通过后显示主界面
                for widget in self.root.winfo_children():
                    widget.destroy()
                self.init_ui()
            else:
                tk.messagebox.showerror('错误', '激活码无效')
                
        form.submit(on_submit)
        # form.pack(fill='both', expand=True, padx=20, pady=20)
        
        return self.verified

if __name__ == '__main__':
    app = AdminFramework('后台框架')
    app.run()